<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/11 0011
 * Time: 14:53
 */

namespace Cuke\Controllers;

use Cuke\Http\ControllerBase;
use Cuke\Models\CukeConnector;
use Cuke\Services\ConnectorService;
use Cuke\Library\Paginator\PaginatorArray;
use Cuke\Services\GroupService;


/**
 * 接口管理
 * @package Cuke\Controllers
 */
class ConnectorController extends ControllerBase
{
    /**
     * 接口列表
     */
    public function listAction()
    {
        $page_index = intval($this->request->getQuery('page',null,1));
        $group_code = trim($this->request->getQuery('group_code'));
        $keyword = trim($this->request->getQuery('keyword'));

        $conditions['conditions'] = 'user_id=:user_id:';
        $conditions['bind']['user_id'] = $this->member->user_id;

        if(empty($group_code) === false){
            $group = GroupService::getInstance()->getGroupDetailed($group_code,$this->member->user_id);
            if(empty($group)){
                $this->dispatcher->forward([ 'controller' => 'Errors', 'action'     => 'show404']);
                return;
            }
            $conditions['conditions'] .= ' AND group_code=:group_code:';
            $conditions['bind']['group_code'] = $group_code;
        }
        if(empty($keyword) === false){
            if(mb_strlen($keyword) >= 100){
                $keyword = mb_substr($keyword,0,100);
            }
            $conditions['conditions'] .= ' AND (connector_name LIKE :connector_name: OR connector_code=:connector_code:)';
            $conditions['bind']['connector_code'] = $keyword;
            $conditions['bind']['connector_name'] = '%'.$keyword.'%';
        }


        $groupResult =   ConnectorService::getInstance()->getConnectorList(1,15,$conditions);
        $paginator = new PaginatorArray([
            'data'          => $groupResult->lists,
            'limit'         => self::PageSize,
            'current'          => $page_index,
            'total_items'   => $groupResult->total_count,
            'href'          => $this->url->get('connector/list'),
            'params'        => []
        ]);

        $this->view->setVar('Model',$paginator);
    }

    public function editAction()
    {
        $connector = new CukeConnector();
        $connector->authenticate = 'APP';
        $connector->protocol = 'HTTP';
        $connector->request_http_method = 'GET';



        if($this->request->isPost()){
            print_r($this->request->getPost());
            exit;
        }
        $connector->connector_id = 10;
        $params = [];

        $this->view->setVar('Parameters',json_encode($params,JSON_UNESCAPED_UNICODE));
        $this->view->setVar('Model',$connector);
    }

}